import { name } from '@/../package'
import { usePermission } from '@/common/config'

const USER_TOKEN_KEY = 'ZK_USER_TOKEN_KEY_' + name
const SYSTEM_ID_KEY = 'ZK_SYSTEM_ID_KEY_' + name
const SYSTEM_THEME_KEY = 'ZK_THEME_KEY_' + name

const LOCAL_VALUE = 'local'

const storage = sessionStorage // localStorage cookie

const getValue = (key) => storage[key] || ''

const setValue = (key, value) => {
  storage[key] = value
}
const clear = key => key ? storage.removeItem(key) : storage.clear()

// 当开启权限  清空本地 token
if (usePermission && getValue(USER_TOKEN_KEY) === LOCAL_VALUE) {
  clear()
}

export default {
  localTokenQuery: `token=${LOCAL_VALUE}&id=${LOCAL_VALUE}`,
  setToken: (token) => setValue(USER_TOKEN_KEY, token),
  getToken: () => getValue(USER_TOKEN_KEY),
  setSystemId: (systemId) => setValue(SYSTEM_ID_KEY, systemId),
  getSystemId: () => getValue(SYSTEM_ID_KEY),
  setTheme: (theme) => setValue(SYSTEM_THEME_KEY, theme),
  getTheme: () => getValue(SYSTEM_THEME_KEY),
  clear,
}
